package cn.com.cis.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import cn.com.cis.domain.Database;
import cn.com.cis.domain.TreeEntity;
import cn.com.cis.service.DataBaseService;
import cn.com.cis.service.TreeService;

public class GenerateXmlUtil {

	private TreeService treeService;

	private DataBaseService dataBaseService;

	StringBuilder	EtlXML	 = new StringBuilder();
	int				LayerNum = 0;

	public String GetJobEtlXml(int nodeid) throws UnsupportedEncodingException {
		//查询当前节点所有子节点
		EtlXML.setLength(0);
		LayerNum = 1;
		EtlXML.append("<!DOCTYPE etl SYSTEM \"http://scriptella.javaforge.com/dtd/etl.dtd\">\r\n<etl>");
		List<TreeEntity> listTreeEntity = treeService.selectTreeByPid(nodeid);
		List<TreeEntity> preList = new ArrayList<TreeEntity>();
		List<TreeEntity> connList = new ArrayList<TreeEntity>();

		if (listTreeEntity != null && listTreeEntity.size() > 0) {
			//把preprity、connection节点集合放到集合的最前面
			for (TreeEntity TreeTemp : listTreeEntity) {
				if (TreeTemp.getMytreetype().equals("properties")) {
					preList.add(TreeTemp);
				} else if (TreeTemp.getMytreetype().equals("connection")) {
					connList.add(TreeTemp);
				}
			}

			listTreeEntity.removeAll(preList);
			listTreeEntity.removeAll(connList);

			//合并
			preList.addAll(connList);
			preList.addAll(listTreeEntity);
		}
		try {
			CreateNodes(preList, "");
		} catch (UnsupportedEncodingException ue) {
			ue.printStackTrace();
		}
		EtlXML.append("\t\r\n</etl>");

		return EtlXML.toString();
	}

	public void CreateNodes(List<TreeEntity> listTreeEntity, String mytreetype) throws UnsupportedEncodingException {

		String TtempStart = "";

		if (!listTreeEntity.isEmpty()) {
			for (TreeEntity treeEntity : listTreeEntity) {
				String tepmytreetype = treeEntity.getMytreetype();
				String tempStr = "";
				String tempComment = "";
				String tempNodeContent = "";
				if (treeEntity.getComment() != null && treeEntity.getComment() != "") {
					tempComment = java.net.URLDecoder.decode(treeEntity.getComment(), "UTF-8");
					//替换大小于号
					tempComment = tempComment.replace("<", "&lt;");
					tempComment = tempComment.replace(">", "&gt;");
					tempComment = tempComment.replace("&lt;!--", "<!--");
					tempComment = tempComment.replace("--&gt;", "-->");
				}
				if (treeEntity.getNodecontent() != null && treeEntity.getNodecontent() != "") {
					tempNodeContent = java.net.URLDecoder.decode(treeEntity.getNodecontent(), "UTF-8");
					tempNodeContent = tempNodeContent.replace("<", "&lt;");
					tempNodeContent = tempNodeContent.replace(">", "&gt;");
					tempNodeContent = tempNodeContent.replace("&lt;!--", "<!--");
					tempNodeContent = tempNodeContent.replace("--&gt;", "-->");
				}

				if (tepmytreetype.equals("properties")) {
					tempNodeContent = tempNodeContent.replace("&lt;include", "<include");
					tempNodeContent = tempNodeContent.replace("&gt;&lt;/include&gt;", " />");
					tempStr = "" + tempComment + "<properties id = \"" + treeEntity.getName() + "\" >" + tempNodeContent + "</properties>";
					EtlXML.append(tempStr);
					continue;
				} else if (tepmytreetype.equals("connection")) {
					//根据connectionid获取database
					Database connDB = null;
					if (!(treeEntity.getConnectionid() == null)) {
						connDB = dataBaseService.selectDatabaseByName(treeEntity.getConnectionid());
					}
					if (connDB != null) {
						tempStr = "" + tempComment + "<connection " + " id = \"" + treeEntity.getName() + "\"" + " url = \"" + connDB.getDatabaseType().getUrl() + "\""
								+ " driver = \"" + connDB.getDatabaseType().getDriver() + "\"" + " user = \"" + connDB.getUsername() + "\"" + " password = \""
								+ connDB.getPassword() + "\"" +
								//" classpath = \"./mysql-connector-java-5.1.34-bin.jar\""+
								" >" + tempNodeContent + "</connection>";
					}
					EtlXML.append(tempStr);
					continue;
				} else if (tepmytreetype.equals("query")) {
					tempStr = TtempStart + tempComment + "<query id=\"" + treeEntity.getName() + "\" connection-id=\"" + treeEntity.getConnectionid() + "\">" + "" + TtempStart
							+ tempNodeContent + "";
					EtlXML.append(tempStr);
					LayerNum++;
				} else if (tepmytreetype.equals("script")) {
					tempStr = TtempStart + tempComment + "<script id=\"" + treeEntity.getName() + "\" connection-id=\"" + treeEntity.getConnectionid() + "\">" + "" + TtempStart
							+ tempNodeContent + "";
					EtlXML.append(tempStr);
					LayerNum++;
				}
				List<TreeEntity> TempListTree = treeService.selectTreeByPid(treeEntity.getId());
				CreateNodes(TempListTree, treeEntity.getMytreetype());
			}
		}
		LayerNum--;
		String TtempEnd = "";
		for (int i = 0; i < LayerNum; i++) {
			TtempEnd += "\t";
		}
		if (mytreetype.equals("query")) {
			EtlXML.append("\r" + TtempEnd + "</query>");
		} else if (mytreetype.equals("script")) {
			EtlXML.append("\r" + TtempEnd + "</script>");
		}
	}

	public void setTreeService(TreeService treeService) {
		this.treeService = treeService;
	}

	public void setDataBaseService(DataBaseService dataBaseService) {
		this.dataBaseService = dataBaseService;
	}

}
